/****************************************************************************/
/* NAME:           Hal_OutputManagement.c                                   */
/* PURPOSE:        Manage all basic input and output                        */
/* CREATED_BY:     Vyx                                                      */
/* CREATION_DATE:  03/07/2012                                               */
/************************************** (C) Copyright 2012 AquaLink *********/

#include "Hal_OutputManagement.h"
#include "Hal_OutputManagement_Config.h"
#include "stdio.h"

#define OUTPUT_NUMBER     (sizeof(OutputConfigList) / sizeof(OutputConfigList[0]))

/**
  * @brief  Main function of input management.
  *         This function is execute one time at the start.
  * @param  none
  * @retval None
  */
void vTaskCTRL_OutputManagementInit(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  uint8_t          i;

  /* init all output pin */
  for (i=0; i < OUTPUT_NUMBER; i++)
  {
    if (OutputConfigList[i].OutputMode == MODE_OUT_AF_PUSH_PULL)
    {
        RCC_APB2PeriphClockCmd(CLOCK_AFIO ,ENABLE);
    }

    /* start clock gpio if is needed */
    if (OutputConfigList[i].OutputGpio == GPIO_A)
    {
       RCC_APB2PeriphClockCmd(CLOCK_GPIOA ,ENABLE);
    }
    if (OutputConfigList[i].OutputGpio == GPIO_B)
    {
       RCC_APB2PeriphClockCmd(CLOCK_GPIOB ,ENABLE);
    }
    if (OutputConfigList[i].OutputGpio == GPIO_C)
    {
       RCC_APB2PeriphClockCmd(CLOCK_GPIOC ,ENABLE);
    }
    if (OutputConfigList[i].OutputGpio == GPIO_D)
    {
        RCC_APB2PeriphClockCmd(CLOCK_GPIOD ,ENABLE);
    }
    if (OutputConfigList[i].OutputGpio == GPIO_E)
    {
        RCC_APB2PeriphClockCmd(CLOCK_GPIOE ,ENABLE);
    }
    if (OutputConfigList[i].OutputGpio == GPIO_F)
    {
        RCC_APB2PeriphClockCmd(CLOCK_GPIOF ,ENABLE);
    }
    if (OutputConfigList[i].OutputGpio == GPIO_G)
    {
        RCC_APB2PeriphClockCmd(CLOCK_GPIOG ,ENABLE);
    }

    GPIO_InitStructure.GPIO_Pin   = OutputConfigList[i].OutputPin;
    GPIO_InitStructure.GPIO_Mode  = OutputConfigList[i].OutputMode;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(OutputConfigList[i].OutputGpio, &GPIO_InitStructure);
  }
}

/**
  * @brief  Get the value of selected output.
  * @param  Name of output
  * @retval Value of output (TRUE or FALSE)
  */
bool OUTPUT_GetOutputValue(uint8_t OutputName)
{
    uint8_t pinValue;
    uint8_t it;

    pinValue = 0;

    for (it = 0; it < OUTPUT_NUMBER; it++)
    {
        if (OutputConfigList[it].OutputName == OutputName)
        {
            pinValue = GPIO_ReadOutputDataBit(OutputConfigList[OutputName].OutputGpio,
                                              OutputConfigList[OutputName].OutputPin);
            break;
        }
    }

    return ((bool)pinValue);
}

/**
  * @brief  Set the value of selected output.
  * @param  Name of output, Output value
  * @retval Value of output (TRUE or FALSE)
  */
void OUTPUT_SetOutputValue(uint8_t OutputName, bool OutputValue)
{
    uint8_t it;

    for (it = 0; it < OUTPUT_NUMBER; it++)
    {
        if (OutputConfigList[it].OutputName == OutputName)
        {
            if ( OutputValue == TRUE )
            {
                GPIO_SetBits(OutputConfigList[OutputName].OutputGpio,
                             OutputConfigList[OutputName].OutputPin);
            }
            else
            {
                GPIO_ResetBits(OutputConfigList[OutputName].OutputGpio,
                               OutputConfigList[OutputName].OutputPin);
            }
            break;
        }
    }
}

